/*
*  Copyright 2019-2020 Zheng Jie
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package com.fzhucc.modules.dataZcs.service;

import com.fzhucc.modules.dataZcs.domain.DataZcs;
import com.fzhucc.utils.ValidationUtil;
import com.fzhucc.utils.FileUtil;
import com.fzhucc.utils.PageUtil;
import com.fzhucc.utils.QueryHelp;
import lombok.RequiredArgsConstructor;
import com.fzhucc.modules.dataZcs.repository.DataZcsRepository;
import com.fzhucc.modules.dataZcs.domain.dto.DataZcsDto;
import com.fzhucc.modules.dataZcs.domain.dto.DataZcsQueryCriteria;
import com.fzhucc.modules.dataZcs.service.mapstruct.DataZcsMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import java.util.List;
import java.util.Map;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.LinkedHashMap;

/**
* @website https://eladmin.vip
* @description 服务实现
* @author yyg
* @date 2024-04-23
**/
@Service
@RequiredArgsConstructor
public class DataZcsService {

    private final DataZcsRepository dataZcsRepository;
    private final DataZcsMapper dataZcsMapper;

    public Map<String,Object> queryAll(DataZcsQueryCriteria criteria, Pageable pageable){
        Page<DataZcs> page = dataZcsRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder),pageable);
        return PageUtil.toPage(page.map(dataZcsMapper::toDto));
    }

    public List<DataZcsDto> queryAll(DataZcsQueryCriteria criteria){
        return dataZcsMapper.toDto(dataZcsRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder)));
    }

    @Transactional
    public DataZcsDto findById(Integer id) {
        DataZcs dataZcs = dataZcsRepository.findById(id).orElseGet(DataZcs::new);
        ValidationUtil.isNull(dataZcs.getId(),"DataZcs","id",id);
        return dataZcsMapper.toDto(dataZcs);
    }

    @Transactional(rollbackFor = Exception.class)
    public DataZcsDto create(DataZcs resources) {
        return dataZcsMapper.toDto(dataZcsRepository.save(resources));
    }

    @Transactional(rollbackFor = Exception.class)
    public void update(DataZcs resources) {
        DataZcs dataZcs = dataZcsRepository.findById(resources.getId()).orElseGet(DataZcs::new);
        ValidationUtil.isNull( dataZcs.getId(),"DataZcs","id",resources.getId());
        dataZcs.copy(resources);
        dataZcsRepository.save(dataZcs);
    }

    public void deleteAll(Integer[] ids) {
        for (Integer id : ids) {
            dataZcsRepository.deleteById(id);
        }
    }

    public void download(List<DataZcsDto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (DataZcsDto dataZcs : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put("值", dataZcs.getValue());
            map.put("日期", dataZcs.getDay());
            map.put("地区orgArea", dataZcs.getOrgArea());
            map.put("日期时间用于查询", dataZcs.getMytime());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }
}